﻿using System;
using System.Collections.Generic;
using System.Web;

using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

/// <summary>
/// Summary description for ConnectDAL
/// </summary>
public class ConnectDAL
{
    private SqlConnection sConn;
    private SqlDataAdapter sDataAp;
    private DataTable tblData;

    public ConnectDAL()
    {
        ConnectDB();
    }

    //phuong thuc ket noi Database
    public void ConnectDB()
    {
        string sql = WebConfigurationManager.ConnectionStrings["AptitudeConnect"].ConnectionString.ToString();

        try
        {

            sConn = new SqlConnection(sql);

        }
        catch (Exception ex) { }
    }

    //Phuong thuc lay du lieu vao DataTable tu CSDL
    public DataTable getData(string sql)
    {

        sDataAp = new SqlDataAdapter(sql, sConn);

        tblData = new DataTable();

        sDataAp.Fill(tblData);

        return tblData;
    }

    //Phuong thuc thuc hien cau lenh Insert,UpDate,Delete tra ve bool
    public bool ExecuteQuery(string sql)
    {
        int count = 0;
        try
        {
            sConn.Open();
            SqlCommand scm = new SqlCommand(sql, sConn);

            count = scm.ExecuteNonQuery();
        }
        catch (Exception ex)
        {

        }
        sConn.Close();
        if (count > 0)
            return true;

        return false;
    }
}